﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SuperMicroWave.Models
{
    public partial class MethodModel : ObservableObject
    {
        public ObservableCollection<HeatFlowModel> HeatFlowItems { get; set; } = new ObservableCollection<HeatFlowModel>();

        public double PrePressure { get; set; }

        public double EndTemp { get; set; }


        public static void WriteMethodXML(string strXmlName, MethodModel dataMethod)
        {
            Directory.CreateDirectory("Xml");//在bin> debug 文件夹下创建名为Xml的文件夹，除非已经存在。

            using (Stream stream = File.Open(strXmlName, FileMode.Create))//在此路径创建一个新xml文件，如果已存在则覆盖
            {
                var serializer = new XmlSerializer(dataMethod.GetType());//这个是关键。根据model的属性创建xml节点
                serializer.Serialize(stream, dataMethod);
            }
        }

        public static bool ReadMethodXML(string strXmlName, ref MethodModel dataMethod)
        {
            //确定指定的文件是否存在
            if (File.Exists(strXmlName))
            {
                using (var stream = File.OpenRead(strXmlName))
                {
                    var serializer = new XmlSerializer(dataMethod.GetType());
                    dataMethod = (MethodModel)serializer.Deserialize(stream)!;
                }

                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
